<?php

class CompanyService extends AbstractService
{

    protected function _init()
    {
	$this->setMapper(new CompanyMapper());
    }

    public function getExtendedData(CompanyModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}
	$this->getCompanyActivity($model);
	$this->getCompanyUser($model);
	$this->getSimilarCompanies($model);
	FactoryService::get('comment')->getByCompany($model);
	FactoryService::get('opinion')->getByCompany($model);
    }

    public function getCompanyUser(CompanyModel $model)
    {
	$userId = $model->getDbData('user_id');
	if (!$userId) {
	    return false;
	}
	$userModel = FactoryService::get('user')->get($userId);
	if ($userModel) {
	    $model->setUser($userModel);
	}
    }

    public function getByUser(UserModel $model, $page = 1, $limit = 10)
    {
	if (!$model->getId()) {
	    return false;
	}

//	$cacheSlot = new CacheSlotsUserFeatures($model, 'companies', array('page' => $page, 'limit' => $limit));
//	if (($companies = $cacheSlot->load()) === false) {
//	    $companies = $this->getMapper()->getByUserId($model->getId(), $page, $limit);
//	    $cacheSlot->setData($companies)->save();
//	}
	$companies = $this->getMapper()->getByUserId($model->getId(), $page, $limit);
	$model->setCompanies($companies);
    }

    public function getCompanyActivity(CompanyModel $model)
    {
	$activityId = $model->getDbData('activity_id');
	if (!$activityId) {
	    return false;
	}
	$activityModel = FactoryService::get('activity')->get($activityId);
	if ($activityModel) {
	    $model->setActivity($activityModel);
	}
    }

    public function getSimilarCompanies(CompanyModel $model)
    {
	if (!$model->getDbData('activity_id')) {
	    return false;
	}
	$cacheSlot = new CacheSlotsCompanyFeatures($model, 'similar');
	if (($similar = $cacheSlot->load()) === false) {
	    $similar = $this->getMapper()->getSimilarCompanies($model->getId(), $model->getDbData('activity_id'));
	    $cacheSlot->setData($similar)->save();
	}
	$model->setSimilarCompanies($similar);
    }

    public function getCompanyList($params, $page = 1, $limit = 10, $dependency = null)
    {
	$params['limit'] = $limit;
	$params['offset'] = ($page - 1) * $limit;
	return $this->getList($params, $dependency);
    }

    public function getCountByUser(UserModel $model)
    {
	if (!$model->getId()) {
	    return false;
	}
	return $this->getCount(array('where' => 'user_id = :user_id', 'params' => array(':user_id' => $model->getId())));
    }

    public function onAfterSave(AbstractModel $model = null)
    {
	if (parent::onAfterSave($model)) {
	    if ($model->getId()) {
		$cacheSlot = new CacheSlotsPk($this->getMapper()->getTableName(), $model->getId());
		$cacheSlot->delete();
		FactoryService::get('search')->toIndex(array(
		    'id' => 'company' . $model->getId(),
		    'type' => 'company',
		    'url' => Yii::app()->createUrl('company/view', array('id' => $model->getId(), 'url' => $model->getDbData('url'))),
		    'text' => $model->getDbData('info'),
		    'title' => $model->getDbData('title')
		));
	    }
	    CacheManager::getCache('memcache')->clear('all.company');
	}
	return true;
    }

    public function onBeforeSave(AbstractModel $model = null)
    {
	if (parent::onBeforeSave($model)) {
	    if (!$model->getId()) {
		$model->url = Translit::encode($model->getDbData('title'));
	    }
	}
	return true;
    }

}

?>
